package daemon;

import org.junit.Test;

/**
 * 守护线程特性测试
 *
 * @author codefish
 * @version 1.0
 * @date 2022/07/22 下午 05:06
 */
public class
DaemonThreadTest {

    /**
     * 当系统中没有用户线程时，JVM不会等待守护线程结束，而是直接关闭进程
     */
    @Test
    public void testDaemonThread (){
        Thread demonThread = new Thread(()->{
            System.out.println("该线程为"+(Thread.currentThread().isDaemon()?"守护线程":"用户线程"));

            while (true){
                //后台线程一直运行
            }
        },"demo");

        demonThread.setDaemon(true);
        demonThread.start();

        try {
            Thread.sleep(2000);
            System.out.println("主线程结束");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
